cmake_minimum_required(VERSION 3.29)
project(untitled2)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_PREFIX_PATH "C:/Qt/6.5.3/msvc2019_64")
set(RESOURCE_PARENT_FOLDER "${CMAKE_SOURCE_DIR}/resource")
file(GLOB_RECURSE QRC_FILES "${RESOURCE_PARENT_FOLDER}/*.qrc")
file(GLOB_RECURSE RC_FILES "${RESOURCE_PARENT_FOLDER}/*.rc")
add_definitions(-DQCUSTOMPLOT_USE_OPENGL)
find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        SerialPort
        OpenGL
        PrintSupport
        REQUIRED)

add_executable(untitled2 main.cpp
        mainwindow.h
        mainwindow.cpp
        receiver.h
        receiver.cpp
        qcustomplot.cpp
        qcustomplot.h
        ${QRC_FILES}
        ${RC_FILES}
        mfmcustomplot.h
        mfmcustomplot.cpp
)
target_link_libraries(untitled2
        Qt::Core
        Qt::Gui
        Qt::Widgets
        Qt6::SerialPort
        Qt6::OpenGL
        Qt6::PrintSupport
        opengl32
        glu32
)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
